import {
  _decorator,
  Component,
  EventKeyboard,
  Input,
  KeyCode,
  input,
} from "cc";
import {
  MovingCommand,
  TurnDirectionCommand,
} from "db://assets/Scripts/Enums/Events/ActionsEnum";
import { PlayerManager } from "db://assets/Scripts/Manager/PlayerManager";
const { ccclass, property } = _decorator;

@ccclass("PlayerKeyBoardAssist")
export class PlayerKeyBoardAssist extends Component {
  onKeyDown(event: EventKeyboard) {
    const playerMgr = this.getComponent(PlayerManager);
    switch (event.keyCode) {
      case KeyCode.ARROW_UP:
        playerMgr.controlMoving(MovingCommand.FORWARD_MOVE);
        break;
      case KeyCode.ARROW_DOWN:
        playerMgr.controlMoving(MovingCommand.BACKWARD_MOVE);
        break;
      case KeyCode.ARROW_LEFT:
        playerMgr.controlMoving(MovingCommand.LEFT_MOVE);
        break;
      case KeyCode.ARROW_RIGHT:
        playerMgr.controlMoving(MovingCommand.RIGHT_MOVE);
        break;
      case KeyCode.KEY_L:
        playerMgr.controlMoving(TurnDirectionCommand.TURN_CLOCK_90);
        break;
      case KeyCode.KEY_R:
        playerMgr.controlMoving(TurnDirectionCommand.TURN_COUNTER_CLOCK_90);
        break;
    }
  }

  onKeyPressing(event: EventKeyboard) {
    const playerMgr = this.getComponent(PlayerManager);
    switch (event.keyCode) {
      case KeyCode.ARROW_UP:
        playerMgr.controlMoving(MovingCommand.FORWARD_MOVE);
        break;
      case KeyCode.ARROW_DOWN:
        playerMgr.controlMoving(MovingCommand.BACKWARD_MOVE);
        break;
      case KeyCode.ARROW_LEFT:
        playerMgr.controlMoving(MovingCommand.LEFT_MOVE);
        break;
      case KeyCode.ARROW_RIGHT:
        playerMgr.controlMoving(MovingCommand.RIGHT_MOVE);
        break;
      case KeyCode.KEY_L:
        playerMgr.controlMoving(TurnDirectionCommand.TURN_CLOCK_90);
        break;
      case KeyCode.KEY_R:
        playerMgr.controlMoving(TurnDirectionCommand.TURN_COUNTER_CLOCK_90);
        break;
    }
  }

  register(target?: any) {
    console.log("KeyBoardAssist-register.target", target);
    input.on(Input.EventType.KEY_DOWN, this.onKeyDown, target);
    input.on(Input.EventType.KEY_PRESSING, this.onKeyPressing, target);
  }

  exit(target?: any) {
    input.off(Input.EventType.KEY_DOWN, this.onKeyDown, target);
    input.off(Input.EventType.KEY_UP, this.onKeyPressing, target);
  }

  start() {
    this._name = "PlayerKeyBoardAssist";
    console.log(this);
  }
}
